package com.dx.blog;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import com.dx.common.base.util.CommonUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.travelsky.hotelbesdk.models.condition.EasySingleHotelReqCondition;
import com.travelsky.hotelbesdk.models.condition.EasySingleHotelReqCondition.ENUM_PAYMENT;

/**
 * BlogController
 * 注意：在实际项目中业务与sql需要写在Model中，此demo仅为示意,故将sql写在了Controller中
 */
@Before(BlogInterceptor.class)
public class BlogController extends Controller {
	
	public void index() {
		 System.out.println("encoding:"+this.getRequest().getCharacterEncoding());
		 //单酒店查询条件类 
		 EasySingleHotelReqCondition condition=new EasySingleHotelReqCondition(); 
		 condition.setCheckindate("2012-08-12"); 
		 condition.setCheckoutdate("2012-08-13"); 
		 condition.setHotelcode("SOHOTO9124"); //大庆测试酒店代码 
		 condition.setPayment(ENUM_PAYMENT.PAYMENT_ALL); //支付方式 

		
		
		setAttr("blogList", Blog.dao.find("select * from blog order by id asc"));
		render("/WEB-INF/jsp/blog/blog.jsp");
	}
	
	public void add() {
	}
	
	@Before(BlogValidator.class)
	public void save() {
		
		System.out.println("title:"+getModel(Blog.class).get("title"));

		System.out.println("content:"+getModel(Blog.class).get("content"));
		getModel(Blog.class).save();
		redirect("/blog");
	}
	
	public void edit() {
		
		setAttr("blog", Blog.dao.findById(getParaToInt()));
		render("/WEB-INF/jsp/blog/edit.jsp");
	}
	
	@Before(BlogValidator.class)
	public void update() throws UnsupportedEncodingException {
		System.out.println("encoding:"+this.getRequest().getCharacterEncoding());
		//this.getRequest().setCharacterEncoding("gbk");
		System.out.println(">>>"+getModel(Blog.class));
		System.out.println("user:"+this.getPara("user"));
		System.out.println("title:"+getModel(Blog.class).get("title"));
		System.out.println("content:"+getModel(Blog.class).get("content"));
		System.out.println("content:"+this.getPara("blog.content"));
		System.out.println("16进制："+CommonUtil.toHexString(this.getPara("blog.content")));
		try {
			System.out.println(new String(this.getPara("blog.content").getBytes(),"GBK"));
			System.out.println(CommonUtil.toStringHex(CommonUtil.toHexString(this.getPara("blog.content"))));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(URLDecoder.decode(this.getPara("blog.content"), "utf8"));
		System.out.println(this.getAttr("blog.content"));
		getModel(Blog.class).update();
		redirect("/blog");
	}
	
	public void delete() {
		Blog.dao.deleteById(getParaToInt());
		redirect("/blog");
	}
}


